<?php

namespace App\Models\Clickhouse\Guanyuan;


use App\Models\Clickhouse\BaseCHModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopOrganizationManageModel;
use App\Models\OpenShop\Organization\OpenShopRegionManageMappingModel;
use App\Models\OpenShop\Organization\OpenShopRegionManageModel;
use App\Service\Kms\Home\HomeService;
use Kuafu\LaravelClickHouse\Database\Eloquent\Builder;

class GyCloseShopCHModel extends BaseCHModel
{
    //【观远】【闭店门店】
    protected $table = "gy_close_shop";

    /**
     * 首页查询条件构造
     *
     * @param int $regionSelectionType
     * @param string $region
     * @return Builder
     */
    public static function buildHomeSearchQuery(int $regionSelectionType, string $region): Builder
    {
        switch ($regionSelectionType) {
            case HomeService::REGION_SELECTION_TYPE_1:
                $query = function ($query) use ($region) {
//                    return $query->where("region", "LIKE", "%" . str_replace("分公司", "", $region) . "%");
                    $organizationModel = OpenShopOrganizationManageModel::query()
                                                                        ->with("regionRel")
                                                                        ->where("name", $region)
                                                                        ->first();

                    if($organizationModel && $organizationModel->regionRel){
                        return $query->whereIn("storeProvince", $organizationModel->regionRel->pluck("provinceName")->toArray());
                    }else{
                        return $query->where("storeProvince", "-1");
                    }
                };
                break;
            case HomeService::REGION_SELECTION_TYPE_2:
                $query = function ($query) use ($region) {
                    return $query->where("storeProvince", $region);
                };
                break;
            case HomeService::REGION_SELECTION_TYPE_3:
                $query = function ($query) use ($region) {
                    return $query->where("storeLocation", $region);
                };
                break;
            case HomeService::REGION_SELECTION_TYPE_4:
                $query = function ($query) use ($region) {
                    $provinceNames = [];
                    $warRegionRow = OpenShopRegionManageModel::query()
                                                             ->with([
                                                                 "regionMappingRel"     => function ($query) {
                                                                     $query->with(["organizationManageRel" => function($query){
                                                                         $query->with("regionRel");
                                                                     }]);
                                                                 }
                                                             ])->where("name", $region)->first();

                    $warRegionRow->regionMappingRel->each(function (OpenShopRegionManageMappingModel $manageMappingModel) use (&$provinceNames) {
                        $provinceNames = array_merge(
                            $provinceNames,
                            $manageMappingModel->organizationManageRel->regionRel->pluck("provinceName")->toArray()
                        );
                    });


                    if($provinceNames){
                        return $query->whereIn("storeProvince", $provinceNames);
                    }else{
                        return $query->where("storeProvince", "-1");
                    }
                };
                break;
            case HomeService::REGION_SELECTION_TYPE_0:
            default:
                $query = null;
                break;
        }
        return self::query()->where("region", "!=", "国外店")
                   ->when($query, function ($subQuery) use ($query) { return $subQuery->where($query); });
    }
}
